package com.bitdf.txing.users.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bitdf.txing.common.enume.TxCodeEnume;
import com.bitdf.txing.common.utils.R;
import com.bitdf.txing.users.entity.UserOtherInfoEntity;
import com.bitdf.txing.users.service.UserOtherInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 
 * @author Lizhiwei
 * @email 123@qq.com
 * @date 2022-09-05 11:22:36
 */
@Slf4j
@RestController
@RequestMapping("${txing.request.path.users}/other/info/")
public class UserOtherInfoController {
    @Autowired
    private UserOtherInfoService userOtherInfoService;


    /**
     * 根据id获取
     */
    @RequestMapping("/{id}")
    public R info(@PathVariable("id") Long userId){
		UserOtherInfoEntity userOtherInfo = userOtherInfoService.getOne(new QueryWrapper<UserOtherInfoEntity>().lambda()
                .eq(UserOtherInfoEntity::getUserId, userId));
        return R.ok(userOtherInfo);
    }

    /**
     * 新建
     */
    @GetMapping("/save")
    public R save(@RequestParam("userId") Long userId){
        UserOtherInfoEntity entity = new UserOtherInfoEntity();
        entity.setUserId(userId);
        entity.setCommentCount(0);
        entity.setLoginCount(0);
        entity.setFansCount(0);
        entity.setAttendCount(0);
        userOtherInfoService.save(entity);
        log.info("用户：{} other 信息保存成功");
        return R.ok();
    }

    /**
     * 修改数量（评论数、关注数等）
     * @param target 修改目标字段
     * @param ops 修改类型
     * @return
     */
    @SaCheckLogin
    @RequestMapping("/update")
    public R update(@RequestParam("userId") Long userId, @RequestParam("target") String target, @RequestParam("ops") int ops){
        if (ops != 1 && ops != -1) {
            return R.error(TxCodeEnume.COMMON_SUBMIT_DATA_EXCEPTION);
        }
        boolean b = userOtherInfoService.updateCount(userId, target, ops);
        return R.ok();
    }

    /**
     * 删除
     */
    @GetMapping("/delete/{id}")
    public R delete(@PathVariable("id") Long userId){
        log.info("用户：{} other信息删除", userId);
        boolean remove = userOtherInfoService.remove(new QueryWrapper<UserOtherInfoEntity>().lambda().eq(UserOtherInfoEntity::getUserId, userId));
        return R.ok();
    }

}
